package cn.iocoder.yudao.module.erp.controller.admin.dp.dpbj;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.erp.controller.admin.dispatchvehicle.vo.DispatchVehicleRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.dispatchvehicle.vo.DispatchVehicleSaveReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpbj.vo.DpBjPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpbj.vo.DpBjRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpbj.vo.DpBjSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dispatchvehicle.DispatchVehicleDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dp.DpBjDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dpitem.DpItemBjDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dplist.DpEastListBjDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dplist.DpWestListBjDO;
import cn.iocoder.yudao.module.erp.service.dp.DpBjService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - ERP北疆口岸仓库制单")
@RestController
@RequestMapping("/erp/dp-bj")
@Validated
public class DpBjController {

    @Resource
    private DpBjService dpBjService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP北疆口岸仓库制单")
//    @PreAuthorize("@ss.hasPermission('erp:dp-bj:create')")
    public CommonResult<String> createDpBj(@Valid @RequestBody DpBjSaveReqVO createReqVO) {
        return success(dpBjService.createDpBj(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP北疆口岸仓库制单")
//    @PreAuthorize("@ss.hasPermission('erp:dp-bj:update')")
    public CommonResult<Boolean> updateDpBj(@Valid @RequestBody DpBjSaveReqVO updateReqVO) {
        dpBjService.updateDpBj(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP北疆口岸仓库制单")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('erp:dp-bj:delete')")
    public CommonResult<Boolean> deleteDpBj(@RequestParam("id") String id) {
        dpBjService.deleteDpBj(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP北疆口岸仓库制单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('erp:dp-bj:query')")
    public CommonResult<DpBjRespVO> getDpBj(@RequestParam("id") String id) {
        DpBjDO dpBj = dpBjService.getDpBj(id);
        return success(BeanUtils.toBean(dpBj, DpBjRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP北疆口岸仓库制单分页")
//    @PreAuthorize("@ss.hasPermission('erp:dp-bj:query')")
    public CommonResult<PageResult<DpBjRespVO>> getDpBjPage(@Valid DpBjPageReqVO pageReqVO) {
        PageResult<DpBjDO> pageResult = dpBjService.getDpBjPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DpBjRespVO.class));
    }

    @GetMapping("/dpBjOuterList")
    @Operation(summary = "获得ERP北疆制单出库列表")
//    @PreAuthorize("@ss.hasPermission('erp:dp-bj:outer')")
    public CommonResult<PageResult<DpBjRespVO>> dpBjOuterList(@Valid DpBjPageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<DpBjDO> dpBjOuterList = dpBjService.dpBjOuterList(pageReqVO);
        return success(BeanUtils.toBean(dpBjOuterList, DpBjRespVO.class));
    }

    @GetMapping("/dpBjVehicleList")
    @Operation(summary = "获得ERP北疆配车列表")
//    @PreAuthorize("@ss.hasPermission('erp:dp-bj:outer')")
    public CommonResult<PageResult<DpBjRespVO>> dpBjVehicleList(@Valid DpBjPageReqVO pageReqVO) {
        PageResult<DpBjDO> dpBjOuterList = dpBjService.dpBjOuterList(pageReqVO);
        return success(BeanUtils.toBean(dpBjOuterList, DpBjRespVO.class));
    }


    @PostMapping("/dpBjOutStroe")
    @Operation(summary = "ERP北疆制单配车出库")
//    @PreAuthorize("@ss.hasPermission('erp:dp-bj:outer')")
    public CommonResult<Boolean> dpBjOutStroe(@Valid @RequestBody DispatchVehicleSaveReqVO createReqVO) {
        dpBjService.dpBjOutStroe(createReqVO);
        return success(true);
    }

    @GetMapping("/dpBjEnterList")
    @Operation(summary = "获得ERP北疆制单入库列表")
//    @PreAuthorize("@ss.hasPermission('erp:dp-bj:enter')")
    public CommonResult<List<DispatchVehicleRespVO>> dpYwEnterList(@RequestParam(value = "dispatchNumber",required = false) String dispatchNumber,
                                                                   @RequestParam(value = "line",required = false) Integer line,
                                                                   @RequestParam(value = "dest",required = false) Integer dest,
                                                                   @RequestParam(value = "dpId",required = false) String dpId) {
        List<DispatchVehicleDO> dpBjEnterList = dpBjService.dpBjEnterList(dispatchNumber,line,1, dest,dpId);
        return success(BeanUtils.toBean(dpBjEnterList, DispatchVehicleRespVO.class));
    }

    @PostMapping("/enterStroeDpBj")
    @Operation(summary = "ERP北疆运单入库")
//    @PreAuthorize("@ss.hasPermission('erp:dp-bj:enter')")
    public CommonResult<Boolean> enterStroeDpBj(@Valid @RequestBody List<DispatchVehicleSaveReqVO> createReqVO) {
        dpBjService.enterStroeDpBj(createReqVO);
        return success(true);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP北疆制单 Excel")
//    @PreAuthorize("@ss.hasPermission('erp:dp-bj:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDpBjExcel(@Valid DpBjPageReqVO pageReqVO,
                                HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DpBjDO> list = dpBjService.getDpBjPage(pageReqVO).getList();
        // 对数据进行字段处理
        List<DpBjRespVO> processedList = list.stream().map(dpBjDO -> {
            DpBjRespVO respVO = BeanUtils.toBean(dpBjDO, DpBjRespVO.class);
            // 格式化创建时间
//            if (dpBjDO.getCreateTime() != null) {
//                respVO.setCreateTimeFormatted(dpBjDO.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
//            }
            // 转换核销字段
            if (dpBjDO.getWriteOff() != null){
                respVO.setWriteOffFormatted(dpBjDO.getWriteOff() == 1 ? "是" : "否");
            }
            // 转换线路字段
            if (dpBjDO.getLine() != null) {
                respVO.setLineFormatted(dpBjDO.getLine() == 1 ? "东线" : "西线");
            }
            return respVO;
        }).collect(Collectors.toList());
        processedList.add(new DpBjRespVO());
        processedList.add(new DpBjRespVO());
        // 创建提示信息行，并将其添加到列表末尾
        DpBjRespVO footerRow = new DpBjRespVO();
        footerRow.setId("注意：" +
                "一：提货时双方当面交接清楚，如货物提走后发现损坏少货现象本公司不负责。\n" +
                "二：预期不提货者，产生一切费用及后果由客户自己承担。\n" +
                "三：易碎的货物在承运过程中发现破碎、丢失时，只赔偿丢失的货物，不赔偿破损的货物。\n" +
                "义乌库房地址:浙江省义乌市苏溪高君路7号(浙江博尼锦纶服份有限公司西)大门进去直走100米311库房 电话/Tel:15600000311(微信同号)\n" +
                "广东库房地址:广东省佛山市南海区里水镇河塱沙里水东部工业园10号南海毛中厂二期(东侧大院311库房) 电话/Te1:18500000311(微信同号)\n" +
                "新疆库房地址:新疆维吾尔自治区乌鲁木齐市沙依巴克区青葱路1048号附1号楼311库 电话/Te1:18199990311(微信同号)");
        // 将提示信息添加到列表末尾
        processedList.add(footerRow);
        // 导出 Excel
        ExcelUtils.write(response, "ERP北疆制单.xls", "数据", DpBjRespVO.class, processedList);
//                        BeanUtils.toBean(processedList, DpYwRespVO.class));
    }

    @GetMapping("/export-list")
    @Operation(summary = "导出ERP北疆制单东西线清单")
//    @PreAuthorize("@ss.hasPermission('erp:dp-bj:exportlist')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDpBjExcelList(@Valid DpBjDO dpBjDO,HttpServletResponse response) throws IOException {
        //获取线路类型
        Integer  line = dpBjDO.getLine();
        List<DpEastListBjDO> dpEastList = dpBjService.getDpEastListBjListByNo(dpBjDO.getNo());
        List<DpWestListBjDO> dpWestList = dpBjService.getDpWestListBjListByNo(dpBjDO.getNo());
        if (line.equals(1)){
            // 导出 Excel东线清单
            ExcelUtils.write(response, "ERP北疆制单东线清单.xls", "清单",DpEastListBjDO.class,
                    BeanUtils.toBean(dpEastList,DpEastListBjDO.class));
        }
        if (line.equals(2)){
            // 导出 Excel西线清单
            ExcelUtils.write(response, "ERP北疆制单西线清单.xls", "清单",DpWestListBjDO.class,
                    BeanUtils.toBean(dpWestList,DpWestListBjDO.class));
        }
    }


    @PutMapping("/examine")
    @Operation(summary = "ERP物流北疆制单审核")
//    @PreAuthorize("@ss.hasPermission('erp:dp-bj:examine')")
    public CommonResult<Boolean> examineDpBj(@Valid @RequestBody DpBjDO dpBjDO){
        dpBjService.examineDpBj(dpBjDO);
        return success(true);
    }

    // ==================== 子表（ERP北疆制单货号详情） ====================

    @GetMapping("/dp-item-bj/list-by-no")
    @Operation(summary = "获得ERP北疆制单货号详情列表")
    @Parameter(name = "no", description = "运单号")
//    @PreAuthorize("@ss.hasPermission('erp:dp-bj:detail')")
    public CommonResult<List<DpItemBjDO>> getDpItemBjListByNo(@RequestParam("no") String no) {
        return success(dpBjService.getDpItemBjListByNo(no));
    }

    // ==================== 子表（北疆东线清单） ====================

    @GetMapping("/dp-east-list-bj/list-by-no")
    @Operation(summary = "获得北疆东线清单列表")
    @Parameter(name = "no", description = "运单号")
//    @PreAuthorize("@ss.hasPermission('erp:dp-bj:detail')")
    public CommonResult<List<DpEastListBjDO>> getDpEastListBjListByNo(@RequestParam("no") String no) {
        return success(dpBjService.getDpEastListBjListByNo(no));
    }

    // ==================== 子表（北疆西线清单） ====================

    @GetMapping("/dp-west-list-bj/list-by-no")
    @Operation(summary = "获得北疆西线清单列表")
    @Parameter(name = "no", description = "运单号")
//    @PreAuthorize("@ss.hasPermission('erp:dp-bj:detail')")
    public CommonResult<List<DpWestListBjDO>> getDpWestListBjListByNo(@RequestParam("no") String no) {
        return success(dpBjService.getDpWestListBjListByNo(no));
    }

}